<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-18 16:40:16
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2022-03-26 23:47:53
 */

namespace app\admin\controller;
use app\common\controller\Backend;

class MerchantRule extends Backend {

	function initialize() {
		parent::initialize();
		$this->saveAndGetConfigIdentify('', '商家菜单', '', 'merchantRule');

		$this->merchantRuleDao = model('MerchantRule');
	}

	public function index() {
		$map = $parameter = array();

		$cid = input('param.cid', '0', 'intval');
		if ($cid) {
			$map['cid'] = $cid;
			$parameter['cid'] = $parameter;
		}
		$this->assign('cid', $cid);

		list($tree) = $this->merchantRuleDao->getTree($map);
		//print_r($tree);
		$this->assign('tree', $tree);

		//获取父级菜单
		$parentList = $this->merchantRuleDao->getParentList();
		$this->assign('parentList', $parentList);

		return $this->fetch();
	}

	public function listorders() {
		$result = parent::baseListorders($this->merchantRuleDao);

		if ($result !== false) {
			$this->success('排序更新成功');
		} else {
			$this->error('排序更新失败');
		}
	}

	public function add() {
		$id = input('param.id', '', 'intval');

		if ($id) {
			//获取菜单数据
			$info = $this->merchantRuleDao->getInfo($id);
			//print_r($info);
		} else {
			$info = [
				'state' 	=> 1,
				'display'	=> 1
			];
		}
		$this->assign('id', $id);
		$this->assign('info', $info);

		//获取父级菜单
		$parentList = $this->merchantRuleDao->getParentList();
		$this->assign('parentList', $parentList);

		return $this->fetch();
	}

	public function doAdd() {
		$id = input('post.id', '', 'intval');
		$data = input('post.data/a', '', 'pwEscape');

		if ($id) {
			$info = $this->merchantRuleDao->getInfo($id);
		}

		//是否有子菜单
		$data['has_sub'] = 0;
		if ($id) {
			$count = $this->merchantRuleDao->countSearch(['pid'=>$id]);
			if ($count) {
				$data['has_sub'] = 1;
			}
		}

		if ($id) {
			$result = $this->merchantRuleDao->baseUpdateData($id, $data);
			
			//更新父菜单
			if ($data['pid']) {
				$this->merchantRuleDao->where('id', $data['pid'])->update(['has_sub'=>1]);
			}
			
			if ($result !== false) {
				$this->success('保存成功', url('admin/merchantRule/index'));
			} else {
				$this->error('保存失败'.showDbError($this->merchantRuleDao));
			}
		} else {
			$result = $this->merchantRuleDao->baseAddData($data);
			
			//更新父菜单
			if ($data['pid']) {
				$this->merchantRuleDao->where(['id'=>$data['pid']])->update(['has_sub'=>1]);
			}
			
			if ($result !== false) {
				$this->success('添加成功', url('admin/merchantRule/index'));
			} else {
				$this->error('添加失败'.showDbError($this->merchantRuleDao));
			}
		}
	}

	public function doDelete() {
		$id = input('param.id', '', 'intval');

		if ($id) {
			$info = $this->merchantRuleDao->getInfo($id);
		}

		$result = $this->merchantRuleDao->del($id);
		if ($result !== false) {
			$this->success('删除成功', url('admin/merchantRule/index'));
		} else {
			$this->error('删除失败'.showDbError($this->merchantRuleDao));
		}
	}

	/**
	 * 根据菜单分类获取父菜单
	 */
	public function getParentByCid() {
		$cid = input('param.cid', '', 'intval');

		//获取父级菜单
		$parentList = $this->merchantRuleDao->getParentListByCid($cid);
		$parentList = array_values($parentList);

		$resultData = [
			'list' => $parentList
		];
		$this->success('', '', $resultData);
	}

}